var WxAutoImage = require('../../js/wxAutoImageCal.js');
var WxParse = require('../wxParse/wxParse.js');
var app = getApp();
Page({
  data: {
    sid: '',
    currentTab: '',
    mc: '',//辅导班名称
    jj: '',//辅导班简介
    fy: 0,//辅导班的费用
    xe: 0,//报名人员限额
    bmrs: 0,//报名人数

    sksj: '',//上课时间
    skdd: '',//上课地点
    bmzlsm: '',//报名资料说明
    
    pris: [],//价格列表
    teas: [],//教师列表

    priarr: [],//被选中的收费
    allprice: 0 //总价格
  },
  cusImageLoad: function (e) {
    var that = this;
    that.setData(WxAutoImage.wxAutoImageCal(e));
  },

  onLoad: function (options) {
    var that = this;
    //获取传递的参数
    this.setData({
      sid: options.sid
    })

    var that = this;
    var openid = wx.getStorageSync('openid');
    if (openid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      that.getdata();
    }
  },
  getdata: function () {//定义函数名称
    wx.showLoading({
      title: '加载中',
    })
    var that = this;

    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/sthd_list.html',//请求地址
      data: {//发送给后台的数据
        sid: that.data.sid
      },
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        that.setData({
          mc: res.data.MC,
          jj: res.data.JJ,
          pris: res.data.pris,
          teas: res.data.teas,
          fy: res.data.FY,
          allprice: res.data.FY,
          xe: res.data.XE,//报名人员限额
          bmrs: res.data.BMRS,//报名人数
          sksj: res.data.SKSJ,//上课时间
          skdd: res.data.SKDD,//上课地点
          bmzlsm: res.data.BMZLSM,//报名资料说明
        });
        wx.hideLoading()
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },

  /** 
    * 点击tab切换 
    */
  swichNavSwi: function (e) {
    var that = this;
    var currentTab = e.target.dataset.current;
    if (this.data.currentTab === currentTab) {
      return false;
    } else {
      that.setData({
        currentTab: currentTab
      })
    }

  },

  niming: function (e) {
    var index = e.target.dataset.index;
    var value = e.detail.value;

    if (value.length != 0) {
      var nps = {};
      var pris = this.data.pris;
      for (var i = 0; i < pris.length; i++) {
        if (pris[i].SFID == value[0]) {
          nps = {
            index: index,
            SFID: pris[i].SFID,
            XMMC: pris[i].XMMC,
            FEE: pris[i].FEE,
          }
        }
      }
      this.data.priarr.push(nps);
    } else {
      var priSarr = [];
      var priarr = this.data.priarr;
      for (var i = 0; i < priarr.length; i++) {
        if (priarr[i].index != index) {
          priSarr.push(priarr[i]);
        }
      }
      this.setData({
        priarr: priSarr
      })
    }

    var allprice = this.data.fy;
    var priarr = this.data.priarr;
    for (var i = 0; i < priarr.length; i++) {
      allprice = parseFloat(allprice) + parseFloat(priarr[i].FEE);
    }
    this.setData({
      allprice: allprice
    })
  },
  
  /** 
    * 购买
    */
  toPay: function (e) {
    if (this.data.xe == this.data.bmrs) {
      wx.showModal({
        title: '友情提示',
        content: "对不起,报名人数已经满额了!",
        showCancel: !1,
      })
    } else {
      wx.navigateTo({
        url: '../../pages/confirmorderst/confirmorderst'//跳转到订单确认页面
      })
    }
  }

})
